# Titled - By: DingYF - 周三 2月 17 2021

import sensor, image, time

red =(38, 48, 47, 73, 27, 56)
green = (9, 87, -67, -11, -18, 66)
blue = (20, 69, -121, 72, -98, -25)
colour = [red,green,blue]
red_blob = None
green_blob = None
blue_blob = None
blobs_group = [red_blob,green_blob,blue_blob]

Name_list = ['R','G','B']
Order_list= [1,2,3]
tool = []
i,j,t= 0,0,2

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
clock = time.clock()

def Find_group():
    t = 0
    while t < 3:
        blobs_group[t] = img.find_blobs([colour[t]])
        if len(blobs_group[t]) > 0:
            for b in blobs_group[t]:
                if  12000 > b[4] > 200:
                    img.draw_rectangle(b[0:4])
                    img.draw_cross(b[5], b[6])
                    img.draw_string(b[0], b[1], Name_list[t])
                    tool.append(b[5])
        t+=1

while (True):
    clock.tick()
    img = sensor.snapshot()
    Order_list= [1,2,3]
    tool = []
    Find_group()
    if len(tool) == 3 :
        for i in range(0,2):
            j=i+1
            for j in range(1,3):
                if tool[i]>tool[j] :
                    tool[i],tool[j] = tool[j],tool[i]
                    Order_list[i],Order_list[j] = Order_list[j],Order_list[i]
                j+=1
            i+=1
        print(Order_list)
        break


def
while(True):
    img = sensor.snapshot()
    img.lens_corr(1.8)
    for code in img.find_qrcodes():
        print(code)
        exit(0)


